package com.company;
import experimentUI.*;
import SelfAssembledPotentiometerByBox.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Circuit1 {
    enum S2State {
    none,Ex,En
    }
    Circuit1UI experimentUI = new Circuit1UI();
    SelfAssembledPotentiometerByBox experimentBox = new SelfAssembledPotentiometerByBox();

    S2State s2State=S2State.none;

    public Circuit1(){
        refreshOutput();
        experimentBox.updateStateAllState();
        //电源与电源输入的监听
        experimentUI.buttonPower.addActionListener(new ActionListener() {
            boolean flag=false;
            @Override
            public void actionPerformed(ActionEvent e) {
                flag=!flag;
                actionPerformedOfPower(flag);
                refreshOutput();
            }
        });
        experimentUI.powerInputConfirm.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionPerformedOfInputPowerText();
                refreshOutput();
            }
        });

        //S1开关监视
        experimentUI.buttonS1.addActionListener(new ActionListener() {
            boolean flag=false;
            @Override
            public void actionPerformed(ActionEvent e) {
                flag=!flag;
                actionPerformedOfS1(flag);
                refreshOutput();
            }
        });

        //S2开关监视
        experimentUI.buttonS2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionPerformedOfS2();
                refreshOutput();
                experimentBox.updateStateAllState();
            }
        });

        //R1电阻箱监视
        experimentUI.buttonR1.addMouseListener(new MouseListener() {
            boolean flag=false;

            @Override
            public void mouseExited(MouseEvent e) {

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton()== MouseEvent.BUTTON1)
                    flag=!flag;
                actionPerformedOfResistanceBox(flag,e,1);
                refreshOutput();
                experimentBox.updateStateAllState();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

        });

        //R2电阻箱监视
        experimentUI.buttonR2.addMouseListener(new MouseListener() {
            boolean flag=false;

            @Override
            public void mouseExited(MouseEvent e) {

            }

            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton()== MouseEvent.BUTTON1)
                    flag=!flag;
                actionPerformedOfResistanceBox(flag,e,2);
                refreshOutput();
                experimentBox.updateStateAllState();
            }

            @Override
            public void mousePressed(MouseEvent e) {

            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }

            @Override
            public void mouseEntered(MouseEvent e) {

            }

        });

        //Ex监视
        experimentUI.buttonEx.addActionListener(new ActionListener() {
            boolean flag=false;
            @Override
            public void actionPerformed(ActionEvent e) {
                flag=!flag;
                actionPerformedOfExOrEn(flag,1);
                refreshOutput();
                experimentBox.updateStateAllState();
            }
        });

        //En监视
        experimentUI.buttonEn.addActionListener(new ActionListener() {
            boolean flag=false;
            @Override
            public void actionPerformed(ActionEvent e) {
                flag=!flag;
                actionPerformedOfExOrEn(flag,2);
                refreshOutput();
                experimentBox.updateStateAllState();
            }
        });

        //G监视
        experimentUI.buttonG.addActionListener(new ActionListener() {
            boolean flag=false;
            @Override
            public void actionPerformed(ActionEvent e) {
                flag=!flag;
                experimentBox.pointerGalvanometer.setConnect(flag);
                experimentUI.buttonG.setContentAreaFilled(!flag);
                refreshOutput();
                experimentBox.updateStateAllState();
            }
        });
    }

    /*
    显示刷新
     */
   private void refreshOutput(){
        experimentUI.outUOfPower.setText("电源电压(V) \t U \t= " + experimentBox.voltageStabilizedPowerSource.getVoltage());
        experimentUI.outIOfPower.setText("电源电流(A) \t I \t= " + experimentBox.voltageStabilizedPowerSource.getElectricity());
        experimentUI.outIOfG.setText("检流计读数(A) \t I \t= " + experimentBox.pointerGalvanometer.getIndicatingValue());
    }

    /*
    电源开关监听的行为
     */
    private void actionPerformedOfPower(boolean flag){
        experimentUI.buttonPower.setContentAreaFilled(!flag);
        experimentUI.powerInputConfirm.setVisible(flag);
        experimentUI.inputPower.setVisible(flag);
        if (flag){
            experimentUI.inputPower.setText("请在此输入电压值点击确认");
            experimentBox.voltageStabilizedPowerSource.pressSwitchButton();
        }else {
            experimentBox.voltageStabilizedPowerSource.releaseSwitchButton();
        }

    }

    /*
    电源输入电压的行为
    */
    //检测输入是否合法
    public static boolean isNumeric(String str) {
        try {
            double num=Double.parseDouble(str);
        } catch (Exception e) {
            return false;//异常 说明包含非数字。
        }
        return true;
    }
    private void actionPerformedOfInputPowerText(){
        String s=experimentUI.inputPower.getText();

        if (isNumeric(s)){
            double num=Double.parseDouble(s);
            if (num>=0&&num<=10){
                experimentBox.voltageStabilizedPowerSource.setVoltage(num);
                System.out.println(num);
            }
            else{
                new ErrorDialog("错误的输入格式 请输入0到10的浮点型数据",experimentUI.mainFrame);

            }
        }else{
            new ErrorDialog("错误的输入格式 请输入0到10的浮点型数据",experimentUI.mainFrame);
        }
    }

    /*
    开关S1的行为
     */
    private void actionPerformedOfS1(boolean flag){
        experimentBox.singleThrowSwitch.setSwitchState(flag);
        if (flag){
            experimentUI.imageS1.setVisible(true);
        }else {
            experimentUI.imageS1.setVisible(false);
        }
        experimentBox.updateStateAllState();
    }

    /*
    转动S2的行为
     */
    private void actionPerformedOfS2(){

        ImageIcon icon = new ImageIcon("src/" + "S2null.png");;
        switch (s2State){
            case En:
                    s2State= S2State.none;
                    experimentBox.doubleThrowSwitch.setSwitchState(0);
                    icon = new ImageIcon("src/" + "S2En.png");
                break;
            case Ex:
                    s2State= S2State.En;
                    experimentBox.doubleThrowSwitch.setSwitchState(2);
                    icon = new ImageIcon("src/" + "S2Ex.png");
                break;
            case none:
                    s2State= S2State.Ex;
                    experimentBox.doubleThrowSwitch.setSwitchState(1);
                    icon = new ImageIcon("src/" + "S2null.png");
                break;
        }
        experimentUI.imageS2.setIcon(icon);
    }

    /*
    电阻箱行为
     */
    private void actionPerformedOfResistanceBox(boolean flag,MouseEvent e,Integer witch){

        if (e.getButton()== MouseEvent.BUTTON1){
            if (witch==1){
                experimentUI.buttonR1.setContentAreaFilled(!flag);
                experimentBox.resistanceBox1.setConnect(flag);
            }else {
                experimentUI.buttonR2.setContentAreaFilled(!flag);
                experimentBox.resistanceBox2.setConnect(flag);
            }

        }else if(flag&&e.getButton()==MouseEvent.BUTTON3){
            resistanceChangeFrame jf = new resistanceChangeFrame("R"+witch+"阻值调节");
            jf.confirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    Integer val= (Integer) jf.jComboBoxOfDigit.getSelectedItem();
                    Integer digit=(Integer) jf.jComboBoxOfVal.getSelectedItem()+1;
                    System.out.println(digit+"\t"+val);
                    Double whole=0.0;
                    if (witch==1){
                        experimentBox.resistanceBox1.setSeparatedResistance(digit,val);
                        whole=experimentBox.resistanceBox1.getResistance();
                    }else {
                        experimentBox.resistanceBox2.setSeparatedResistance(digit,val);
                        whole=experimentBox.resistanceBox2.getResistance();
                    }
                    String str=String.format("当前电阻%.1f",whole);
                    jf.resistanceNum.setText(str);
                }
            });
        }
    }

    /*
    Ex/En行为
     */
    private void actionPerformedOfExOrEn(boolean flag,Integer witch){
        if (witch==1){
            experimentUI.buttonEx.setContentAreaFilled(!flag);
            experimentBox.unknownBattery.setConnect(flag);
        }else {
            experimentUI.buttonEn.setContentAreaFilled(!flag);
            experimentBox.standardBattery.setConnect(flag);
        }
    }
}
